//
//  ManHuaDetailsViewController.m
//  Double
//
//  Created by 孟令博 on 15/5/23.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "ManHuaDetailsViewController.h"
#import <AFNetworking/AFHTTPRequestOperationManager.h>
#import <UIImageView+WebCache.h>
#import "ManHuaDetailsCell.h"
#import "ManHuaDetailsModel.h"
#import "MJRefresh.h"
#import "ManHuaPlayerViewController.h"



@interface ManHuaDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>
//存放model的数组
@property (nonatomic, retain) NSMutableArray *bigArray;
@property (nonatomic, assign) int i;
@end

@implementation ManHuaDetailsViewController
- (void)dealloc
{
    [_ID release];
    [_bigArray release];
    [_table1 release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.bigArray = [NSMutableArray array];
        self.i = 2;
    }
    return self;
}

#pragma mark -
#pragma mark 请求数据
- (void)startConnection
{
    NSString *httpStr = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=typeList&id=%@&page=1&limit=30", self.ID];
    
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:httpStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        
        NSArray *bigArray = [dic objectForKey:@"list"];
        for (NSDictionary *dic in bigArray) {
//            NSLog(@"%@", dic);
            ManHuaDetailsModel *model = [[ManHuaDetailsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
            [model release];
        }
        [self.table1 reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

- (void)MyTableView
{
    self.table1 = [[ADCustomTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    //设置代理人
    _table1.dataSource = self;
    _table1.delegate = self;
    
    self.table1.separatorStyle = UITableViewCellStyleDefault;
    //设定tableView重用池
    [self.table1 registerClass:[ManHuaDetailsCell class] forCellReuseIdentifier:@"cell"];
//设定上啦加载下来刷新
    [self.table1 addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData:)];
    [self.table1 addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.view addSubview:_table1];
    [_table1 release];
}
// 下拉刷新点击事件
- (void)loadNewData:(id)sender
{
//    [self.bigArray removeAllObjects];
//    [self.table1 reloadData];
    [self startConnection];
    [self.table1.header endRefreshing];
}
// 上啦加载点击事件
- (void)loadMoreData
{
//page值 i++
    NSString *httpStr = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=typeList&id=%@&page=%d&limit=30", self.ID,self.i];
    
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:httpStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        
        NSArray *bigArray = [dic objectForKey:@"list"];
        if (bigArray.count == 0) {
            [self.table1 removeFooter];
            return;
        }
        for (NSDictionary *dic in bigArray) {
            ManHuaDetailsModel *model = [[ManHuaDetailsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
            [model release];
        }
        [self.table1 reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    self.i += 1;
    [self.table1.footer endRefreshing];
}
#pragma mark -
#pragma mark tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.bigArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ManHuaDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ManHuaDetailsModel *model = [self.bigArray objectAtIndex:indexPath.row];
    //    [cell.image setImage:model.pic];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageRetryFailed];
    [cell.label setText:model.name];
//    [cell.label1 setText:model.lastup];
// 判断是否完结
    if (model.isOver == 1) {
        [cell.label1 setText:[NSString stringWithFormat:@"全%@" ,model.lastup]];
    } else {
        [cell.label1 setText:[NSString stringWithFormat:@"更新到第%@" ,model.lastup]];
    }
    [cell.lable2 setText:[NSString stringWithFormat:@"%@分", model.score]];
    return cell;
}
//cellgaodu
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ManHuaPlayerViewController *player = [[ManHuaPlayerViewController alloc] init];
    ManHuaDetailsModel *model = [self.bigArray objectAtIndex:indexPath.row];
    player.iD = model.MyId;
    player.episode = model.lastup;
    if (model.isOver == 1) {
        
        player.episode = [NSString stringWithFormat:@"全%@" ,model.lastup];
        
    } else {
        player.episode =[NSString stringWithFormat:@"更新到第%@" ,model.lastup];
    }
    player.navigationItem.title = model.name;
    player.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:player animated:YES];
    [player release];
    
    }


- (void)viewDidLoad {
    [super viewDidLoad];
    [self MyTableView];
    [self startConnection];
  //自定义的返回按钮
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftbuttonAction)]autorelease];
    

}


- (void)leftbuttonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
